﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Konves.Csv.CsvSerialization
{
    public class CsvSerializer
    {
        public CsvSerializer(Type type) : this(type, true, CsvEscapeOptions.AutoEscape) { }
        public CsvSerializer(Type type, bool includeHeader): this(type, includeHeader, CsvEscapeOptions.AutoEscape) { }
        public CsvSerializer(Type type, CsvEscapeOptions options) : this(type, true, options) { }
        public CsvSerializer(Type type, bool includeHeader, CsvEscapeOptions options)
        {
            this.Type = type;
            this.IncludeHeader = includeHeader;
            this.EscapeOptions = options;
        }

        public Type Type { get; private set; }
        public bool IncludeHeader { get; private set; }
        public CsvEscapeOptions EscapeOptions { get; private set; }

        public object Deserialize(string csv)
        {
            return null;
        }

        public void Serialize(TextWriter textWriter, object o)
        {
            string t = string.Empty;

            
        }

        public List<List<string>> Parse(string data, char delimiter, char escape) //, out List<long> linebreaks, out List<long> fieldbreaks)
        {
            //linebreaks = new List<long>();
            //fieldbreaks = new List<long>();
            
            bool beforeEsc = true;
            bool inEsc = false;
            bool afterEsc = false;
            bool canEsc = true;

            char d = delimiter;
            char e = escape;

            char cr = '\r';
            char lf = '\n';

            int datastarts = 0;

            List<int> delimiters = new List<int>();

            for (int i = 0; i < data.Length; i++)
            {
                if (beforeEsc)
                {
                    if (Char.IsWhiteSpace(data[i])) // whitespce
                    {
                    }
                    else if (data[i] == d) // delimiter
                    {
                        beforeEsc = true;
                        inEsc = false;
                        afterEsc = false;
                        canEsc = true;
                    }
                }
                else if (inEsc)
                {

                }
                else if (afterEsc)
                {
                    if (Char.IsWhiteSpace(data[i])) // whitespce
                    {

                    }
                    else if (data[i] == d) // delimiter
                    {
                        //reset bools
                        beforeEsc = true;
                        inEsc = false;
                        afterEsc = false;
                        canEsc = true;

                        // log delimiter
                        delimiters.Add(i);
                    }
                    else if (data[i] == cr && data.Length > i && data[i] == lf) // linebreak
                    {
                        //reset bools
                        beforeEsc = true;
                        inEsc = false;
                        afterEsc = false;
                        canEsc = true;

                        // log delimiter
                        delimiters.Add(i);

                        // datastarts after linebreak
                        datastarts = i + 2;
                    }
                    else // other text
                    {
                        throw new Exception(string.Format("Delimiter or whitespace expected: Index {0}", i));
                    }
                }

                //if (beforeEsc)
                //{
                //    if (Char.IsWhiteSpace(data[i])) // whitespce
                //    {
                //        // log start in case if not escaped
                //        datastarts = i;
                //    }
                //    else if (data[i] == d) // delimiter
                //    {
                //        // field is null
                //        inEsc = false;
                //        useEsc = false;
                //        beforeEsc = true;
                //        afterEsc = false;

                //    }
                //    else if (data[i] == e) // escape character
                //    {
                //        // is escaped, data starts next

                //    }
                //    else // other
                //    {
                //        // is not escaped, data starts now
                //    }
                //}
            }
        }
    }
}
